package com.zynn.service.module.es.service;

import com.zynn.common.pojo.dto.zuul.LogInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * @author 刘天元
 * @description
 * @create 2018-07-01 10:47
 **/
@Service
@Slf4j
public class SyncLogService {

    @Autowired
    private RestHighLevelClient client;

    public void syncOneLog(LogInfoDTO logInfoDTO) {
        Map<String, Object> jsonMap = new HashMap<>(20);
        jsonMap.put("request_id", logInfoDTO.getRequestId());
        jsonMap.put("user_id", logInfoDTO.getUserId());
        jsonMap.put("jwt", logInfoDTO.getJwt());
        jsonMap.put("request_url", logInfoDTO.getRequestUrl());
        jsonMap.put("method", logInfoDTO.getMethod());
        jsonMap.put("user_agent", logInfoDTO.getUserAgent());
        jsonMap.put("params", logInfoDTO.getParams());
        jsonMap.put("start_time", logInfoDTO.getStartTime());
        jsonMap.put("end_time", logInfoDTO.getEndTime());
        jsonMap.put("time", logInfoDTO.getTime());
        jsonMap.put("create_time", logInfoDTO.getCreateTime());
        jsonMap.put("exception", logInfoDTO.getException());
        jsonMap.put("response_status_code", logInfoDTO.getResponseStatusCode());
        jsonMap.put("redirect_host", logInfoDTO.getRedirectHost());
        jsonMap.put("redirect_port", logInfoDTO.getRedirectPort());
        jsonMap.put("response_data", logInfoDTO.getResponseData());
        jsonMap.put("version", logInfoDTO.getVersion());
        jsonMap.put("source", logInfoDTO.getSource());

        //Map会自动转换为JSON格式的文档源
        IndexRequest indexRequest = new IndexRequest("log_index", "_doc")
                .source(jsonMap);
        try {
            IndexResponse indexResponse = client.index(indexRequest);
            ///log.info(indexResponse.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public Integer countErrorAPI(int minute) {
        Integer errorAPICount = 0;
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // 拼装publish_time条件
        RangeQueryBuilder publishTimeRangeQueryBuilder = QueryBuilders.rangeQuery("publish_time");
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -minute);
        Date time = beforeTime.getTime();
        publishTimeRangeQueryBuilder.gt(time);
        boolQueryBuilder.must(publishTimeRangeQueryBuilder);
        //拼装返回状态异常条件
        BoolQueryBuilder exceptionApiBoolQueryBuilder = QueryBuilders.boolQuery();
        RangeQueryBuilder timeRangeQueryBuilder = QueryBuilders.rangeQuery("time");
        timeRangeQueryBuilder.gt(1000);
        exceptionApiBoolQueryBuilder.should(timeRangeQueryBuilder);
        BoolQueryBuilder errorApiBoolQueryBuilder = QueryBuilders.boolQuery();
        errorApiBoolQueryBuilder.mustNot(QueryBuilders.constantScoreQuery(QueryBuilders.termsQuery("response_status_code", Arrays.asList(200, 401))));
        exceptionApiBoolQueryBuilder.should(errorApiBoolQueryBuilder);
        boolQueryBuilder.must(exceptionApiBoolQueryBuilder);
        searchSourceBuilder.query(boolQueryBuilder);
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.source(searchSourceBuilder);
        searchRequest.indices("log_index");

        try {
            SearchResponse searchResponse = client.search(searchRequest);
            errorAPICount = (int) searchResponse.getHits().getTotalHits();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return errorAPICount;
    }


}
